/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('HotelsListCtrl', ['$scope', '$cordovaActionSheet', '$state', 'CrmHttp', function($scope, $cordovaActionSheet, $state, CrmHttp) {

    var type = $state.params.type;
    $scope.title = type === 'public' ? '公海' : '私海';

    // 酒店列表
    $scope.hotelList = [];

    // 下拉刷新
    $scope.doRefresh = function() {
        if ($scope.hotelList.length >= 30) {
            $scope.hotelList = [];
        }
        loadingMoreHotel();
        // 停止广播ion-refresher
        $scope.$broadcast('scroll.refreshComplete');
    };

    // 是否要加载更多
    $scope.moreDataCanBeLoaded = function() {
        return $scope.hotelList.length > 30 ? false: true;
    };

    // 加载更多
    $scope.loadingMore = function() {
        loadingMoreHotel();
        // 停止广播ion-infinite
        $scope.$broadcast('scroll.infiniteScrollComplete');
    };

    // 显示酒店明细
    $scope.showHotelDetail = function(hotelId) {
        $state.go('tab.hotel-detail', {hotelId: hotelId});
    };

    // 显示酒店电话列表
    $scope.showTelList = function($event, telList) {
        $scope.hotelTelList = telList;
        var options = {
            title: '电话列表',
            buttonLabels: telList,
            addCancelButtonWithLabel: '取消',
            androidEnableCancelButton : true,
            winphoneEnableCancelButton : true
            // addDestructiveButtonWithLabel : 'Delete it'
        };
        $cordovaActionSheet.show(options).then(function(btnIndex) {
            // 取消按钮
            if (telList.length + 1 > btnIndex) {
                location.href = 'tel:' + telList[btnIndex - 1];
            }
        });
        $event.stopPropagation();
    };

    // 加载更多
    function loadingMoreHotel() {
        // 酒店列表
        for (var i = 0; i < 10; i++) {
            $scope.hotelList.push({
                id: i,
                status: '未上线',
                flow: '客服审核',
                name: '重庆和悦酒店-' + i,
                address: '重庆市秀山土家族苗族自治区丹凤路3号',
                charge: '张三',
                type: '房间已确认',
                tels: ['010-68686688', '13241999999']
            });
        }
    };
    // 初始化调用
    loadingMoreHotel();
}]);